class MyPromise {
    constructor(fn) {
        this.resolvedCallbacks = [];
        this.rejectedCallbacks = [];
        this.state = "PENDING"; // PENDING REDOLVED REJECTED
        this.value = "";
        fn(this.resolve.bind(this), this.reject.bine(this));
    }


    resolve(value) {
        if (this.state == "PENDING") {
            this.state = "RESOLVED";
            this.value = value;
            this.resolvedCallbacks.map(cb => cb(value))
        }
    }

    reject(value) {
        if (this.state == "PENDING") {
            this.state = "REJECTED";
            this.value = value;
            this.rejectedCallbacks.map(cb => cb(value))
        }
    }

    then(onFulfilled, onRejected) {
        if(this.state == "PENDING" ){
            this.resolvedCallbacks.push(onFulfilled)
            this.rejectedCallbacks.push(onRejected)
        }
        if(this.state == "RESOLVED"){
            onFulfilled(this.value)
        }
        if(this.state == "REJECTED"){
            onRejected(this.value)
        }

    }
}

// 具体的Promise A+规范请阅读 v1详细版本
